
//
//  MeituanRefreshControl.swift
//  自定义刷新控件
//
//  Created by YYY on 2017/11/1.
//  Copyright © 2017年 成品家（北京）网络科技有限公司. All rights reserved.
//

import UIKit
//下拉临界点设置
private let kPullUpOffsetHeight: CGFloat = 120
//下拉状态
enum PullUpState {
    //普通状态
    case Normal
    //下拉状态
    case Pulling
    //刷新状态
    case Refreshing
}

class MeituanRefreshControl: UIControl {
    private weak var scrollView: UIScrollView?
    private lazy var refreshView = MeituanRefreshView()
    
    init()
    {
        super.init(frame: CGRect())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        guard let tempView = newSuperview as? UIScrollView else {
            return
        }
        scrollView = tempView
        
        //添加监听方法
        scrollView?.addObserver(self, forKeyPath: "contentOffset", options: [], context: nil)
    }
    
    //实现监听代理
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        guard let sv = scrollView else {
            return
        }
        let newHeight = -(sv.contentInset.top + sv.contentOffset.y)
        if newHeight < 0
        {
            return
        }

        if refreshView.refershState != .Refreshing
        {
            refreshView.parmentHeight = newHeight
        }
        
        frame = CGRect(x: 0, y: -newHeight, width: sv.bounds.width, height: newHeight)
        refreshView.setTop(newTop: newHeight - refreshView.height)
        if sv.isDragging
        {
            if height < kPullUpOffsetHeight && refreshView.refershState == .Pulling
            {
                refreshView.refershState = .Normal
            }
            else if height > kPullUpOffsetHeight && refreshView.refershState == .Normal
            {
                refreshView.refershState = .Pulling
            }
        }
        else
        {
            if refreshView.refershState == .Pulling
            {
                beginRefreshing()
                sendActions(for: .valueChanged)
            }
        }
    }
    
    //移除监听者
    override func removeFromSuperview()
    {
        superview?.removeObserver(self, forKeyPath: "contentOffset")
        super.removeFromSuperview()
    }
    //开始刷新
    func beginRefreshing()
    {
        if refreshView.refershState == .Refreshing
        {
            return
        }
        
        guard let sv = scrollView else {
            return
        }
        refreshView.refershState = .Refreshing
        var inset = sv.contentInset
        inset.top += kPullUpOffsetHeight
        sv.contentInset = inset
        
        refreshView.parmentHeight = kPullUpOffsetHeight
    }
    
    //结束刷新
    func endRefreshing()
    {
        guard let sv = scrollView else {
            return
        }
        if refreshView.refershState != .Refreshing
        {
            return
        }
        refreshView.refershState = .Normal
        var inset = sv.contentInset
        inset.top -= kPullUpOffsetHeight
        sv.contentInset = inset
    }
}
extension MeituanRefreshControl
{
    func setupUI()
    {
        //添加刷新视图
        addSubview(refreshView)
    }
}
